home *** CD-ROM | disk | FTP | other *** search
/ American Osteopathic Ass…tion Yearbook 2005 & 2006 / American Osteopathic Association Yearbook 2005 & 2006.iso / mac / app / dis.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2004-07-22  |  7.2 KB  |  259 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. '''Disassembler of Python byte code into mnemonics.'''
  5. import sys
  6. import types
  7. from opcode import *
  8. from opcode import __all__ as _opcodes_all
  9. __all__ = [
  10.     'dis',
  11.     'disassemble',
  12.     'distb',
  13.     'disco'] + _opcodes_all
  14. del _opcodes_all
  15.  
  16. def dis(x = None):
  17.     '''Disassemble classes, methods, functions, or code.
  18.  
  19.     With no argument, disassemble the last traceback.
  20.  
  21.     '''
  22.     if x is None:
  23.         distb()
  24.         return None
  25.     
  26.     if type(x) is types.InstanceType:
  27.         x = x.__class__
  28.     
  29.     if hasattr(x, 'im_func'):
  30.         x = x.im_func
  31.     
  32.     if hasattr(x, 'func_code'):
  33.         x = x.func_code
  34.     
  35.     if hasattr(x, '__dict__'):
  36.         items = x.__dict__.items()
  37.         items.sort()
  38.         for name, x1 in items:
  39.             if type(x1) in (types.MethodType, types.FunctionType, types.CodeType, types.ClassType):
  40.                 print 'Disassembly of %s:' % name
  41.                 
  42.                 try:
  43.                     dis(x1)
  44.                 except TypeError:
  45.                     msg = None
  46.                     print 'Sorry:', msg
  47.  
  48.                 print 
  49.                 continue
  50.         
  51.     elif hasattr(x, 'co_code'):
  52.         disassemble(x)
  53.     elif isinstance(x, str):
  54.         disassemble_string(x)
  55.     else:
  56.         raise TypeError, "don't know how to disassemble %s objects" % type(x).__name__
  57.  
  58.  
  59. def distb(tb = None):
  60.     '''Disassemble a traceback (default: last traceback).'''
  61.     if tb is None:
  62.         
  63.         try:
  64.             tb = sys.last_traceback
  65.         except AttributeError:
  66.             raise RuntimeError, 'no last traceback to disassemble'
  67.  
  68.         while tb.tb_next:
  69.             tb = tb.tb_next
  70.     
  71.     disassemble(tb.tb_frame.f_code, tb.tb_lasti)
  72.  
  73.  
  74. def disassemble(co, lasti = -1):
  75.     '''Disassemble a code object.'''
  76.     code = co.co_code
  77.     byte_increments = [ ord(c) for c in co.co_lnotab[0::2] ]
  78.     line_increments = [ ord(c) for c in co.co_lnotab[1::2] ]
  79.     table_length = len(byte_increments)
  80.     lineno = co.co_firstlineno
  81.     table_index = 0
  82.     while table_index < table_length and byte_increments[table_index] == 0:
  83.         lineno += line_increments[table_index]
  84.         table_index += 1
  85.         continue
  86.         []
  87.     addr = 0
  88.     line_incr = 0
  89.     labels = findlabels(code)
  90.     n = len(code)
  91.     i = 0
  92.     extended_arg = 0
  93.     free = None
  94.     while i < n:
  95.         c = code[i]
  96.         op = ord(c)
  97.         if i >= addr:
  98.             lineno += line_incr
  99.             while table_index < table_length:
  100.                 addr += byte_increments[table_index]
  101.                 line_incr = line_increments[table_index]
  102.                 table_index += 1
  103.                 if line_incr:
  104.                     break
  105.                     continue
  106.                 []
  107.                 continue
  108.                 []
  109.             addr = sys.maxint
  110.             print '%3d' % lineno,
  111.         else:
  112.             print '   ',
  113.         if i == lasti:
  114.             print '-->',
  115.         else:
  116.             print '   ',
  117.         if i in labels:
  118.             print '>>',
  119.         else:
  120.             print '  ',
  121.         print `i`.rjust(4), opname[op].ljust(20),
  122.         i = i + 1
  123.         if op >= HAVE_ARGUMENT:
  124.             oparg = ord(code[i]) + ord(code[i + 1]) * 256 + extended_arg
  125.             extended_arg = 0
  126.             i = i + 2
  127.             if op == EXTENDED_ARG:
  128.                 extended_arg = oparg * 0x10000L
  129.             
  130.             print `oparg`.rjust(5),
  131.             if op in hasconst:
  132.                 print '(' + `co.co_consts[oparg]` + ')',
  133.             elif op in hasname:
  134.                 print '(' + co.co_names[oparg] + ')',
  135.             elif op in hasjrel:
  136.                 print '(to ' + `i + oparg` + ')',
  137.             elif op in haslocal:
  138.                 print '(' + co.co_varnames[oparg] + ')',
  139.             elif op in hascompare:
  140.                 print '(' + cmp_op[oparg] + ')',
  141.             elif op in hasfree:
  142.                 if free is None:
  143.                     free = co.co_cellvars + co.co_freevars
  144.                 
  145.                 print '(' + free[oparg] + ')',
  146.             
  147.         
  148.         print 
  149.  
  150.  
  151. def disassemble_string(code, lasti = -1, varnames = None, names = None, constants = None):
  152.     labels = findlabels(code)
  153.     n = len(code)
  154.     i = 0
  155.     while i < n:
  156.         c = code[i]
  157.         op = ord(c)
  158.         if op == opmap['SET_LINENO'] and i > 0:
  159.             print 
  160.         
  161.         if i == lasti:
  162.             print '-->',
  163.         else:
  164.             print '   ',
  165.         if i in labels:
  166.             print '>>',
  167.         else:
  168.             print '  ',
  169.         print `i`.rjust(4), opname[op].ljust(15),
  170.         i = i + 1
  171.         if op >= HAVE_ARGUMENT:
  172.             oparg = ord(code[i]) + ord(code[i + 1]) * 256
  173.             i = i + 2
  174.             print `oparg`.rjust(5),
  175.             if op in hasconst:
  176.                 if constants:
  177.                     print '(' + `constants[oparg]` + ')',
  178.                 else:
  179.                     print '(%d)' % oparg,
  180.             elif op in hasname:
  181.                 if names is not None:
  182.                     print '(' + names[oparg] + ')',
  183.                 else:
  184.                     print '(%d)' % oparg,
  185.             elif op in hasjrel:
  186.                 print '(to ' + `i + oparg` + ')',
  187.             elif op in haslocal:
  188.                 if varnames:
  189.                     print '(' + varnames[oparg] + ')',
  190.                 else:
  191.                     print '(%d)' % oparg,
  192.             elif op in hascompare:
  193.                 print '(' + cmp_op[oparg] + ')',
  194.             
  195.         
  196.         print 
  197.  
  198. disco = disassemble
  199.  
  200. def findlabels(code):
  201.     '''Detect all offsets in a byte code which are jump targets.
  202.  
  203.     Return the list of offsets.
  204.  
  205.     '''
  206.     labels = []
  207.     n = len(code)
  208.     i = 0
  209.     while i < n:
  210.         c = code[i]
  211.         op = ord(c)
  212.         i = i + 1
  213.         if op >= HAVE_ARGUMENT:
  214.             oparg = ord(code[i]) + ord(code[i + 1]) * 256
  215.             i = i + 2
  216.             label = -1
  217.             if op in hasjrel:
  218.                 label = i + oparg
  219.             elif op in hasjabs:
  220.                 label = oparg
  221.             
  222.             if label >= 0:
  223.                 if label not in labels:
  224.                     labels.append(label)
  225.                 
  226.             
  227.         label >= 0
  228.     return labels
  229.  
  230.  
  231. def _test():
  232.     '''Simple test program to disassemble a file.'''
  233.     if sys.argv[1:]:
  234.         if sys.argv[2:]:
  235.             sys.stderr.write('usage: python dis.py [-|file]\n')
  236.             sys.exit(2)
  237.         
  238.         fn = sys.argv[1]
  239.         if not fn or fn == '-':
  240.             fn = None
  241.         
  242.     else:
  243.         fn = None
  244.     if fn is None:
  245.         f = sys.stdin
  246.     else:
  247.         f = open(fn)
  248.     source = f.read()
  249.     if fn is not None:
  250.         f.close()
  251.     else:
  252.         fn = '<stdin>'
  253.     code = compile(source, fn, 'exec')
  254.     dis(code)
  255.  
  256. if __name__ == '__main__':
  257.     _test()
  258.  
  259.